package com.hualan.controller;

import com.github.pagehelper.PageInfo;
import com.hualan.pojo.ExpertiseField;
import com.hualan.pojo.Result;
import com.hualan.service.ExpertiseFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/hdx")
public class ExpertiseFieldController {
    @Autowired
    private ExpertiseFieldService expertiseFieldService;

    @GetMapping("selectField")
    public Result selectField(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize, String fieldName){
        PageInfo<ExpertiseField> expertiseFieldPageInfo = expertiseFieldService.selectField(pageNum, pageSize, fieldName);
        return Result.success(expertiseFieldPageInfo);
    }
    @PostMapping("/insertField")
    public Result insertField(@RequestBody ExpertiseField field){
        int i = expertiseFieldService.insertField(field);
        if(i != 0){
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }
    @DeleteMapping("/deleteField")
    public Result deleteField(Long id){
        int i = expertiseFieldService.deleteField(id);
        if (i != 0){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }
    @GetMapping("/updateField")
    public Result updateField(Long id,String fieldName){
        int i = expertiseFieldService.updateField(id, fieldName);
        if (i != 0){
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }
    @DeleteMapping("/deleteFields")
    public Result deleteFields(@RequestBody List<Integer> ids){
        int i = expertiseFieldService.deleteBatch(ids);
        if (i != 0){
            return Result.success("批量删除成功");
        }
        return Result.error("批量删除失败");
    }
    @PostMapping("/findField")
    public Result findField(){
        List<ExpertiseField> expertiseFields = expertiseFieldService.find();
        return Result.success(expertiseFields);
    }
}
